<?php
     function getParameter($pname){
          return isset($_POST[$pname])?$_POST[$pname]:"";
     }

     $adminurl = "https://maple2.neweb.com.tw:443/CashSystemFrontEnd/Query";
     $code = "abcd1234";
     $merchantnumber     = getParameter('merchantnumber');
     $ordernumber        = getParameter('ordernumber');
     $writeoffnumber     = getParameter('writeoffnumber');
     $paymenttype        = getParameter('paymenttype');
     $timecreateds       = getParameter('timecreateds');
     $timecreatede       = getParameter('timecreatede');
     $timepaids          = getParameter('timepaids');
     $timepaide          = getParameter('timepaide');
     $status             = getParameter('status');
     $operation          = getParameter('operation');
     
     $Year = date('Y');
     $month = date('m');
     $Day = date('d');
     $Hour = date('H');
     $Minute = date('i');
     $Second = date('s');

     $time = $Year.$month.$Day.$Hour.$Minute.$Second;

     $hash = md5($operation.$code.$time);

     $postdata = "merchantnumber=".$merchantnumber."&ordernumber=".$ordernumber."&writeoffnumber=".$writeoffnumber.
                 "&paymenttype=".$paymenttype."&timecreateds=".$timecreateds."&timecreatede=".$timecreatede.
                 "&timepaids=".$timepaids."&timepaide=".$timepaide."&status=".$status.
                 "&operation=".$operation."&time=".$time."&hash=".$hash;

     $url = parse_url($adminurl);

     $postdatalen = strlen($postdata);
     $postdata = "POST ".$url['path']." HTTP/1.0\r\n".
                "Content-Type: application/x-www-form-urlencoded\r\n".
                "Host: ".$url['host'].":".$url['port']."\r\n".
                "Content-Length: ".$postdatalen."\r\n".
                "\r\n".
                $postdata;

     $receivedata = "";

     //-- 若不用SSL(https)連接，則改為 $fp = fsockopen ($url['host'], $url['port'], $errno, $errstr, 90);
     $fp = fsockopen ("ssl://".$url['host'], $url['port'], $errno, $errstr, 90);

     //$fp = fsockopen ($hostip, $hostport, &$errno, &$errstr, 90);
     if(!$fp) { 
          echo "$errstr ($errno)<br>\n";
     }else{ 
          fputs ($fp, $postdata);

          do{ 
               if(feof($fp)){
                    //echo "connect is break\n";
                 	break;
               }
               $tmpstr = fgets($fp,128);
               $receivedata = $receivedata.$tmpstr;
          }while(true); //!($tmpstr=="0")
          fclose ($fp);
     }

     $receivedata = str_replace("\r","",trim($receivedata));
     $isbody = false;
     $httpcode = null;
     $httpmessage = null;
     $result = "";
     $array1 = split("\n",$receivedata);
     for($i=0;$i<count($array1);$i++){
          if($i==0){
               $array2 = split(" ",$array1[$i]);
               $httpcode = $array2[1];
               $httpmessage = $array2[2];
          }else if(!$isbody){
               if(strlen($array1[$i])==0) $isbody = true;
          }else{
               $result = $result.$array1[$i];
          }
     }

     if($httpcode!="200"){
          if($httpcode=="404") echo "網址錯誤，無法找到網頁!";
          else if($httpcode=="500") echo "伺服器錯誤!";
          else echo $httpmessage;
          return;
     }

     $rc = null;
     $rc2 = null;
     $orders = array();
     $array1 = split("&",$result);
     for($i=0,$ocount = -1;$i<count($array1);$i++){
          $array2 = split("=",$array1[$i]);
          if($i==0){
               $rc = $array2[1];
               if($rc!="0"){
                    $array2 = split("=",$array1[++$i]);
                    if($array2[0]=="rc2") $rc2 = $array2[1];
               }
          }else{
               if($array2[0]=="merchantnumber"){
                    $orders[++$ocount] = array();
                    $orders[$ocount][0] = $array2[1];
               }else if($array2[0]=="ordernumber"){
                    $orders[$ocount][1] = $array2[1];
               }else if($array2[0]=="serialnumber"){
                    $orders[$ocount][2] = $array2[1];
               }else if($array2[0]=="amount"){
                    $orders[$ocount][3] = $array2[1];
               }else if($array2[0]=="paymenttype"){
                    $orders[$ocount][4] = $array2[1];
               }else if($array2[0]=="writeoffnumber"){
                    $orders[$ocount][5] = $array2[1];
               }else if($array2[0]=="status"){
                    $orders[$ocount][6] = $array2[1];
               }else if($array2[0]=="timecreated"){
                    $orders[$ocount][7] = $array2[1];
               }else if($array2[0]=="timepaid"){
                    $orders[$ocount][8] = $array2[1];
               }else if($array2[0]=="paycount"){
                    $orders[$ocount][9] = $array2[1];
               }else if($array2[0]=="paidamount"){
                    $orders[$ocount][10] = $array2[1];
               }
          }
     }
?>
<html>
<head>
<script>
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>簡單付查詢訂單範例</title>
</head>
<body>
<center>
<table border=1 cellpadding=0 cellspacing=0 bordercolor="#dddddd" width="100%">
     <tr>
          <td colspan=9>回覆碼：<?php echo $rc ?>，次回覆碼：<?php echo $rc2 ?></td>
     </tr>
<?php     if($rc=="0"){ ?>
     <tr>
          <td>商家編號</td>
          <td>訂單編號</td>
          <td>訂單序號</td>
          <td>金額</td>
          <td>付款方式</td>
          <td>銷帳編號</td>
          <td>訂單狀態</td>
          <td>建立時間</td>
          <td>繳款時間</td>
          <td>繳費次數</td>
          <td>繳費總金額</td>
     </tr>
<?php
               for($i=0;$i<count($orders);$i++){
?>
     <tr>
          <td><?php echo $orders[$i][0]; ?></td>
          <td><?php echo $orders[$i][1]; ?></td>
          <td><?php echo $orders[$i][2]; ?></td>
          <td><?php echo $orders[$i][3]; ?></td>
          <td><?php echo $orders[$i][4]; ?></td>
          <td><?php echo $orders[$i][5]; ?></td>
          <td><?php echo $orders[$i][6]; ?></td>
          <td><?php echo $orders[$i][7]; ?></td>
          <td><?php echo $orders[$i][8]; ?></td>
          <td><?php echo $orders[$i][9]; ?></td>
          <td><?php echo $orders[$i][10]; ?></td>
     </tr>
<?php
               }
          }
?>
</table>
</center>
</body>
</html>
